package com.qf.controller;

import com.qf.entity.Student;
import com.qf.service.StudentService;
import com.qf.utils.Constants;
import com.qf.vo.ResultVo;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/student")
public class studentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/pic")
    public void pic(String picname , HttpServletResponse response) throws IOException {
        System.out.println("接收到的参数为："+picname);
        String studentPicDir = Constants.STUDENT_PIC_DIR;
        File file = new File(studentPicDir + picname);
//        将文件的内容返回(将文件中的内容放到响应体中)
        IOUtils.copy(new FileInputStream(file),response.getOutputStream());
       //在响应中获取到file数据
    }
    @PostMapping("/up")
    public ResultVo uploadPic(MultipartFile file) throws IOException {
//        file.getOriginalFilename() 获取文件名
        System.out.println("接收到的文件名为:"+file.getOriginalFilename());
        //指定地址存储路径
        String studentPicDir = Constants.STUDENT_PIC_DIR;
//      文件名 (避免重名) UUID是java.util提供的类 randomUUID() 随机的文件名
        String fileName = UUID.randomUUID().toString();
//        处理文件的扩展名 .jpg
        String oldName = file.getOriginalFilename();
//        从尾部查找.返回下标
        int index = oldName.lastIndexOf(".");
//        从下标开始截取到字符串结束就是扩展字符串
        String extName = oldName.substring(index);
//        字符串拼接组成图片的保存位置
        file.transferTo(new File(studentPicDir+fileName+extName));
//        响应数据中获取到img
        String url = "http://192.168.1.49:8080/student/pic?picname="+(fileName+extName);
        return new ResultVo(200, "ok", url);
    }

    @GetMapping("/list")
    public ResultVo list() {
        List<Student> data = studentService.list();
        return new ResultVo(200,"ok",data);
    }

    @PostMapping("/add")
    //注意如果是post请求，并且前端发送过来的参数是请求体中的json,
    //需要使用对象收参。并且在对象前面添加 @RequestBody
    public ResultVo add(@RequestBody Student student){
        System.out.println("接收到的参数为:"+student);
        List<Student> data = studentService.add(student);
        return new ResultVo(200,"ok",data);
    }

    @PostMapping("/edit")
    public ResultVo edit(@RequestBody Student student){
        System.out.println("接收的到的参数为:"+student);
        studentService.edit(student);
        return new ResultVo(200,"ok","修改成功");
    }

    @DeleteMapping("/delete")
    public ResultVo delete(Integer id){
        System.out.println("接收的到的参数为:"+id);
        studentService.delete(id);
        return new ResultVo(200,"ok","删除成功");
    }

    @GetMapping("/finbyId")
    public ResultVo findbyId(Integer id){

        System.out.println("接收的到的参数为:"+id);
        Student student = studentService.findbyId(id);
        return new ResultVo(200,"ok",student);
    }
}
